package com.liusy.taskengine.executemodel;
import java.sql.Connection;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.logicalcobwebs.proxool.ProxoolFacade;
import org.logicalcobwebs.proxool.admin.SnapshotIF;


import com.liusy.jdbc.JdbcHelp;
import com.liusy.taskengine.TaskEngineConstant;
import com.liusy.taskengine.taskexecute.AbstractTaskExecute;
import com.liusy.taskengine.taskmodel.ITaskModel;

/**
 * 数据库执行者接口
 * 
 *  @author liusy 2012-12-11
 */
public class DBExecuteModel extends  AbstractExecuteModel{

	private static Logger log = Logger.getLogger(DBExecuteModel.class);
	
	//连接池中存在的活动连接
	protected int curActiveCount=0;
	
	//充许最大的连接
	protected int maxCount=0;
	
	//可以得到连接的数
	protected int availableCount=0;
	
	//数据库连接
	protected Connection cn = null;
	
	//执行器完成1000 任务，关闭一次连接 由于数据库连接一直未关闭，时间长proxool会自己关，长时间用 一个连接对数据库性能不好
	protected int taskNumber = 0;
	
	//数据库查sql处理询类
	protected Statement s=null;
	
	public DBExecuteModel()
	{
		
	}
	
	public DBExecuteModel(String executeModelName)
	{
		super(executeModelName);
	}
	
	
	/*
	 * 执行任务
	 * 
	 * (non-Javadoc)
	 * @see com.liusy.taskengine.AbstractExecuteModel#RunTask(com.liusy.taskengine.ITaskModel, com.liusy.taskengine.ITaskExecute)
	 */
	@Override
	public List<Map<String,Object>> RunTask(ITaskModel task) {
		List<Map<String,Object>> list =null;
		try {
			++taskNumber;
				SnapshotIF snapshot = ProxoolFacade.getSnapshot(TaskEngineConstant.COMPARISON_TASK_CONNECTION, false);
				 curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数
				  maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数
				  availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数
				  //ProxoolFacade.shutdown();
				if(curActiveCount>=maxCount-20)
				{
					log.info("获得活动连接数: "+curActiveCount+"    获得总连接数："+maxCount+"     获得可得到的连接数"+availableCount);
					log.info(this.executeModelName+"    活动连接数已经超超过最大，不能继续执行...正在等待其它兄弟释放连接.......");
					log.info("请修改链接池的配置信息...........");
				}
				else
				{	if(null==cn||cn.isClosed())
					{
							cn  = JdbcHelp.getJdbcHelp().getConnection(TaskEngineConstant.COMPARISON_TASK_CONNECTION);
							if(null!=cn)
							s = cn.createStatement();
					}
					if(null==cn||null==s)
						return list;
					list= DBTask(task);

					exeRsult=1;
						
					ProxoolFacade.killConnecton(cn, false);
				}
	
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return list;
	}
	
	
	/*
	 * 数据库任务具体处理方法
	 * 
	 */
	public List<Map<String,Object>> DBTask(ITaskModel task)
	{
		AbstractTaskExecute _taskExecute = (AbstractTaskExecute) taskExecute;
		try {
			log.debug(_taskExecute.getName()+":.............正在执行任务.....目前连接池数:"+curActiveCount);
			List<Map<String,Object>> list=JdbcHelp.getDataObject(((ITaskModel)taskModel).getSql(), this.s);
			log.debug(_taskExecute.getName()+":.............任务完成.....目前连接池数:"+curActiveCount);
			return list;
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
	}
	

	public int getCurActiveCount() {
		return curActiveCount;
	}

	public void setCurActiveCount(int curActiveCount) {
		this.curActiveCount = curActiveCount;
	}

	public int getMaxCount() {
		return maxCount;
	}

	public void setMaxCount(int maxCount) {
		this.maxCount = maxCount;
	}
	
	
	
}
